** Preparing CIA-based extrapolation

use "$project_path/data/4_regdata/regdata", clear
gen decade = Year - mod(Year, 10)
egen region_decade = group(Region decade)
tab region_decade, gen(d_region_decade)
local outcomes = "diff_m1_chix_econ std_diff_m1_hdi std_diff_m1_democracy_index diff_m1_general_chix"
local controls = "d_region_decade*"
keep if abs(runvar)<=10

keep Country Year Month Type_Election runvar treatment `controls' `outcomes'

tempfile ar_data
save `ar_data'

** Appendix Figure E.6

foreach v in `outcomes' {
	
	use `ar_data', clear
	
	keep if `v'!=.
	
	reg `v' `controls' if treatment == 0
	predict pred0, xb
	reg `v' `controls' if treatment == 1
	predict pred1, xb

	lpoly pred0 runvar, degree(1) kernel(triangle) generate(fit0) at(runvar) nograph
	lpoly pred1 runvar, degree(1) kernel(triangle) generate(fit1) at(runvar) nograph
	
	egen temp_x = cut(runvar), at(-100(2)100) 
	replace temp_x = temp_x  + 1
	sort temp_x Country Year Month Type_Election
	by temp_x: egen temp_y = mean(`v')
	by temp_x: egen temp_fit0 = mean(fit0) // Mean of stored smoothing grid in fit0
	by temp_x: egen temp_fit1 = mean(fit1) // Mean of stored smoothing grid in fit1
	by temp_x: egen temp_pred0 = mean(pred0)
	by temp_x: egen temp_pred1 = mean(pred1)
	by temp_x: gen temp_i = _n
					
	if "`v'"=="diff_m1_chix_econ" local title = "Economic performance"
	if "`v'"=="std_diff_m1_hdi" local title = "Human Development Index"
	if "`v'"=="std_diff_m1_democracy_index" local title = "Democracy"
	if "`v'"=="diff_m1_general_chix" local title = "Country performance"

	// We plot the CIA-based estimates of expected potential outcomes around the threshold 
	graph twoway (scatter temp_y temp_x if abs(temp_x) <= 10 & temp_i == 1) ///
		(scatter temp_pred1 temp_x if abs(temp_x) <= 10 & runvar < 0 & temp_i == 1, msymbol(x) mcolor(none) ) ///
		(scatter temp_pred0 temp_x if abs(temp_x) <= 10 & runvar >= 0 & temp_i == 1, msymbol(x) mcolor(red) msize(*2)) ///
		(lpoly fit0 runvar if abs(runvar) <= 10 & runvar < 0, degree(1) kernel(triangle) lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(lpoly fit1 runvar if abs(runvar) <= 10 & runvar >= 0, degree(1) kernel(triangle) lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(lpoly fit1 runvar if abs(runvar) <= 10 & runvar < 0, degree(1) kernel(triangle) lpattern(shortdash) lwidth(medthick) lcolor(none)) ///
		(lpoly fit0 runvar if abs(runvar) <= 10 & runvar >= 0, degree(1) kernel(triangle) lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
		, legend(order(4 7) size(large) region(lwidth(none)) label(4 "Fitted") label(7 "Extrapolated")) xtitle("Margin of victory of the challenger", size(large)) ytitle("Standard deviations", size(large)) ///
		xline(0) xlabel(-10(5)10, labsize(large)) ylabel(, angle(0) labsize(large)) graphregion(color(white) fcolor(white)) plotregion(color(white) fcolor(white)) plotregion(style(none)) title("`title'") 
		
	if "`v'"=="diff_m1_chix_econ" local figure_name = "Appendix_Figure_E6a"
	if "`v'"=="std_diff_m1_hdi" local figure_name = "Appendix_Figure_E6b"
	if "`v'"=="std_diff_m1_democracy_index" local figure_name = "Appendix_Figure_E6c"
	if "`v'"=="diff_m1_general_chix" local figure_name = "Appendix_Figure_E6d"

	graph export "$project_path/output/appendix_figures/`figure_name'.pdf", as(pdf) name("Graph") replace
	
	drop pred* temp* fit*
	
}

